package org.nfyszny.zzzs.modules.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoDict;
import org.jeecg.common.system.query.QueryGenerator;
import org.nfyszny.zzzs.modules.entity.ZzzsProductRecording;
import org.nfyszny.zzzs.modules.service.IZzzsProductRecordingService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: 产品备案
 * @Author: jeecg-boot
 * @Date: 2022-11-11
 * @Version: V1.0
 */
@Api(tags = "产品备案")
@RestController
@RequestMapping("/modules/zzzsProductRecording")
@Slf4j
public class ZzzsProductRecordingController extends JeecgController<ZzzsProductRecording, IZzzsProductRecordingService> {
    @Autowired
    private IZzzsProductRecordingService zzzsProductRecordingService;

    /**
     * 产品变更
     *
     * @param zzzsProductRecording
     * @return
     */
    @AutoLog(value = "产品变更-修改备案")
    @ApiOperation(value = "产品变更-修改备案", notes = "产品变更-修改备案")
    //@RequiresPermissions("org.nfyszny.zzzs:zzzs_record_of_supply_ability:delete")
    @PutMapping(value = "/change")
    public Result<String> change(@RequestBody ZzzsProductRecording zzzsProductRecording) {
        zzzsProductRecordingService.change(zzzsProductRecording);
        return Result.TOK("删除成功!");
    }

    /**
     * 产品追溯追责
     */
    @GetMapping("retroactiveAccountability/{productId}")
    @AutoLog("产品追溯追责")
    public Result retroactiveAccountability(@PathVariable String productId) {
        Map<String, Object> map = zzzsProductRecordingService.retroactiveAccountability(productId);
        return Result.TOK(map);

    }

    /**
     * 年审
     *
     * @param zzzsProductRecording
     * @return
     */
    @AutoLog(value = "产品备案-修改备案")
    @ApiOperation(value = "产品备案-修改备案", notes = "产品备案-修改备案")
    //@RequiresPermissions("org.nfyszny.zzzs:zzzs_record_of_supply_ability:delete")
    @PutMapping(value = "/review")
    public Result<String> review(@RequestBody ZzzsProductRecording zzzsProductRecording) {
        zzzsProductRecordingService.review(zzzsProductRecording);
        return Result.TOK("删除成功!");
    }

    /**
     * 分页列表查询
     *
     * @param zzzsProductRecording
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "产品备案-分页列表查询")
    @ApiOperation(value = "产品备案-分页列表查询", notes = "产品备案-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<ZzzsProductRecording>> queryPageList(ZzzsProductRecording zzzsProductRecording,
                                                             @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                             @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                             HttpServletRequest req) {
        QueryWrapper<ZzzsProductRecording> queryWrapper = QueryGenerator.initQueryWrapper(zzzsProductRecording, req.getParameterMap());
        Page<ZzzsProductRecording> page = new Page<ZzzsProductRecording>(pageNo, pageSize);
        IPage<ZzzsProductRecording> pageList = zzzsProductRecordingService.page(page, queryWrapper);
        return Result.TOK(pageList);
    }

    /**
     * 根据公司id分页列表查询
     *
     * @param
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "企业备案-分页列表查询")
    @ApiOperation(value = "产品备案-根据公司id分页列表查询", notes = "产品备案-根据公司id分页列表查询")
    @GetMapping(value = "/getListByCompanyId")
    @AutoDict
    public Result<IPage<ZzzsProductRecording>> getListByCompanyId(ZzzsProductRecording zzzsProductRecording,
                                                                  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                                  HttpServletRequest req) {
        QueryWrapper<ZzzsProductRecording> queryWrapper = QueryGenerator.initQueryWrapper(zzzsProductRecording, req.getParameterMap());
        Page<ZzzsProductRecording> page = new Page<ZzzsProductRecording>(pageNo, pageSize);
        IPage<ZzzsProductRecording> pageList = zzzsProductRecordingService.getListByCompanyId(queryWrapper, page);
        return Result.TOK(pageList);
    }

    @ApiOperation(value = "产品备案-根据公司id分页列表查询", notes = "产品备案-根据公司id分页列表查询")
    @GetMapping(value = "/getExitAll")
    @AutoDict
    public Result<IPage<ZzzsProductRecording>> exitAll(ZzzsProductRecording zzzsProductRecording,
                                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                       HttpServletRequest req) {
        QueryWrapper<ZzzsProductRecording> queryWrapper = QueryGenerator.initQueryWrapper(zzzsProductRecording, req.getParameterMap());
        Page<ZzzsProductRecording> page = new Page<ZzzsProductRecording>(pageNo, pageSize);
        IPage<ZzzsProductRecording> pageList = zzzsProductRecordingService.getExitaLL(queryWrapper, page);
        return Result.TOK(pageList);
    }

    @ApiOperation(value = "产品备案-根据公司id分页列表查询", notes = "产品备案-根据公司id分页列表查询")
    @GetMapping(value = "/getExitList")
    @AutoDict
    @AutoLog(value = "证书表-准出列表")
    public Result<IPage<ZzzsProductRecording>> getExitList(ZzzsProductRecording zzzsProductRecording,
                                                           @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                           HttpServletRequest req) {
        QueryWrapper<ZzzsProductRecording> queryWrapper = QueryGenerator.initQueryWrapper(zzzsProductRecording, req.getParameterMap());
        Page<ZzzsProductRecording> page = new Page<ZzzsProductRecording>(pageNo, pageSize);
        IPage<ZzzsProductRecording> pageList = zzzsProductRecordingService.getExitList(queryWrapper, page);
        return Result.TOK(pageList);
    }

    @ApiOperation(value = "产品备案-根据公司id分页列表查询", notes = "产品备案-根据公司id分页列表查询")
    @GetMapping(value = "/getCanApply")
    @AutoDict
    public Result<IPage<ZzzsProductRecording>> canApply(ZzzsProductRecording zzzsProductRecording,
                                                        @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                        HttpServletRequest req) {
        QueryWrapper<ZzzsProductRecording> queryWrapper = QueryGenerator.initQueryWrapper(zzzsProductRecording, req.getParameterMap());
        Page<ZzzsProductRecording> page = new Page<ZzzsProductRecording>(pageNo, pageSize);
        IPage<ZzzsProductRecording> pageList = zzzsProductRecordingService.getCanApply(queryWrapper, page);
        return Result.TOK(pageList);
    }

    @GetMapping("traceSource/{productId}")
    public Result traceSource(@PathVariable String productId) {
        Map<String, Object> map = zzzsProductRecordingService.traceSource(productId);
        return Result.TOK(map);
    }

    /**
     * 申请准出
     *
     * @param
     * @return
     */
    @AutoLog(value = "证书表-准出")
    @ApiOperation(value = "证书表-编辑", notes = "证书表-编辑")
    //@RequiresPermissions("org.nfyszny.zzzs:zzzs_certificate:edit")

    @RequestMapping(value = "/applyForExit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> applyForExit(@RequestBody ZzzsProductRecording zzzsProductRecording) {
        zzzsProductRecordingService.applyForExit(zzzsProductRecording);
        return Result.TOK("编辑成功!");
    }

    /**
     * 准出
     *
     * @param
     * @return
     */
    @AutoLog(value = "证书表-准出")
    @ApiOperation(value = "证书表-编辑", notes = "证书表-编辑")
    //@RequiresPermissions("org.nfyszny.zzzs:zzzs_certificate:edit")
    @RequestMapping(value = "/yesExit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> yesExit(@RequestBody ZzzsProductRecording zzzsProductRecording) {
        zzzsProductRecordingService.yesExit(zzzsProductRecording);
        return Result.TOK("编辑成功!");
    }

    /**
     * 不予准出
     *
     * @param
     * @return
     */
    @AutoLog(value = "证书表-编辑")
    @ApiOperation(value = "证书表-编辑", notes = "证书表-编辑")
    //@RequiresPermissions("org.nfyszny.zzzs:zzzs_certificate:edit")
    @RequestMapping(value = "/noExit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> noExit(@RequestBody ZzzsProductRecording zzzsProductRecording) {
        zzzsProductRecordingService.noExit(zzzsProductRecording);
        return Result.TOK("编辑成功!");
    }

    @ApiOperation(value = "产品备案-列表查询", notes = "产品备案-列表查询")
    @GetMapping(value = "/listInfo")
    public Result<List<ZzzsProductRecording>> getList() {
        List<ZzzsProductRecording> zzzsProductRecordings = zzzsProductRecordingService.getListInfo();
        return Result.TOK(zzzsProductRecordings);
    }

    @ApiOperation(value = "产品备案-列表查询", notes = "产品备案-列表查询")
    @GetMapping(value = "/getCreateListByCompanyId")
    public Result<IPage<ZzzsProductRecording>> getCreateListByCompanyId(ZzzsProductRecording zzzsProductRecording,
                                                                        @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                                        HttpServletRequest req) {

        QueryWrapper<ZzzsProductRecording> queryWrapper = QueryGenerator.initQueryWrapper(zzzsProductRecording, req.getParameterMap());
        Page<ZzzsProductRecording> page = new Page<ZzzsProductRecording>(pageNo, pageSize);
        IPage<ZzzsProductRecording> pageList = zzzsProductRecordingService.getCreateListByCompanyId(queryWrapper, page);
        return Result.TOK(pageList);

    }

    /**
     * 添加
     *
     * @param zzzsProductRecording
     * @return
     */
    @AutoLog(value = "产品备案-新增备案")
    @ApiOperation(value = "产品备案-新增备案", notes = "产品备案-新增备案")
    //@RequiresPermissions("org.nfyszny.zzzs:zzzs_product_recording:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody ZzzsProductRecording zzzsProductRecording) {
        zzzsProductRecordingService.saveProductRecordInfo(zzzsProductRecording);
        return Result.TOK("添加成功！");
    }

    @AutoLog(value = "产品备案-提交备案")
    @ApiOperation(value = "产品备案-提交备案", notes = "产品备案-提交备案")
    //@RequiresPermissions("org.nfyszny.zzzs:zzzs_product_recording:add")
    @PutMapping(value = "/submitRecord")
    public Result<String> submitRecord(@RequestBody ZzzsProductRecording zzzsProductRecording) {
        zzzsProductRecordingService.submitRecord(zzzsProductRecording);
        return Result.TOK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param zzzsProductRecording
     * @return
     */
    @AutoLog(value = "产品备案-编辑")
    @ApiOperation(value = "产品备案-编辑", notes = "产品备案-编辑")
    //@RequiresPermissions("org.nfyszny.zzzs:zzzs_product_recording:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody ZzzsProductRecording zzzsProductRecording) {
        zzzsProductRecordingService.updateById(zzzsProductRecording);
        return Result.TOK("编辑成功!");
    }


    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "产品备案-通过id删除")
    @ApiOperation(value = "产品备案-通过id删除", notes = "产品备案-通过id删除")
    //@RequiresPermissions("org.nfyszny.zzzs:zzzs_product_recording:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        zzzsProductRecordingService.removeById(id);
        return Result.TOK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "产品备案-批量删除")
    @ApiOperation(value = "产品备案-批量删除", notes = "产品备案-批量删除")
    //@RequiresPermissions("org.nfyszny.zzzs:zzzs_product_recording:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.zzzsProductRecordingService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.TOK("批量删除成功!");
    }

    /**
     * getHaveInfoById
     */
    @ApiOperation(value = "产品备案-查询已备案完成的备案信息", notes = "产品备案-查询已备案完成的备案信息")
    @GetMapping(value = "/getHaveInfoById")
    public Result<ZzzsProductRecording> getHaveInfoById(@RequestParam(name = "id", required = true) String id) {
        ZzzsProductRecording zzzsProductRecording = zzzsProductRecordingService.getHaveInfoById(id);
        if (zzzsProductRecording == null) {
            return Result.error("未找到对应数据");
        }
        return Result.TOK(zzzsProductRecording);
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "产品备案-通过id查询")
    @ApiOperation(value = "产品备案-通过id查询", notes = "产品备案-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<ZzzsProductRecording> queryById(@RequestParam(name = "id", required = true) String id) {
        ZzzsProductRecording zzzsProductRecording = zzzsProductRecordingService.getById(id);
        if (zzzsProductRecording == null) {
            return Result.error("未找到对应数据");
        }
        return Result.TOK(zzzsProductRecording);
    }


    /**
     * 导出excel
     *
     * @param request
     * @param zzzsProductRecording
     */
    //@RequiresPermissions("org.nfyszny.zzzs:zzzs_product_recording:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ZzzsProductRecording zzzsProductRecording) {
        return super.exportXls(request, zzzsProductRecording, ZzzsProductRecording.class, "产品备案");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    //@RequiresPermissions("zzzs_product_recording:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ZzzsProductRecording.class);
    }

}
